home *** CD-ROM | disk | FTP | other *** search
/ BBS in a Box 3 / BBS in a box - Trilogy III.iso / Files / Prog / L-M / MacOberon 4.0 / MacOberon™ 4.0 Folder / DK.MacOberonApp.Text (.txt) < prev    next >
Encoding:
Oberon Text  |  1993-10-25  |  15.3 KB  |  143 lines  |  [.Ob./.Ob5]

  1. Syntax10.Scn.Fnt
  2. ParcElems
  3. Alloc
  4. Times18.Scn.Fnt
  5. Times12.Scn.Fnt
  6. Syntax14.Scn.Fnt
  7. Syntax12.Scn.Fnt
  8. Times14.Scn.Fnt
  9. Syntax14b.Scn.Fnt
  10. Times12i.Scn.Fnt
  11.     Monaco9.Scn.Fnt
  12. Writing Macintosh Applications using MacOberon
  13. Oliver Dreer, Michael Franz, 25.10.93
  14. This document explains how to create stand-alone Macintosh applications using MacOberon. Since MacOberon's compiler is a fast and efficient 32-bit compiler that produces dense code, applications generated by MacOberon will often run faster and usually be smaller than similar programs developed using other programming environments.
  15. Introduction
  16. We assume that you know and appreciate the Oberon language and the MacOberon compiler already. So why not use them to develop standard Macintosh applications? In fact, MacOberon can be used to create such applications. The main concern here is how to gain access to the routines, types and constants of the Macintosh Toolbox.
  17. The package of interface files distributed with this document contains the more important routines and types, and some constants of the Macintosh Toolbox. They are grouped in modules structured along the lines of the new Inside Macintosh series of books. Unfortunately, by the time this is released, not all of the new Inside Macintosh books are yet available, so that some stuff may still be in the wrong place. However, the interface files that we supply are plaintext source files and can be updated easily.
  18. The Macintosh Toolbox
  19. Using the Macintosh Toolbox from within MacOberon is slightly different from other programming environments such as MPW or Think Pascal. Most importantly, the data in MacOberon's interface files is structured similar to the new Inside Macintosh books (from now on IM). There are many toolbox calls that have changed their spelling in the new IMs, for example the routine that extracts a menu text used to be called GetItem and is now called GetMenuItemText. In most cases, we still use the old spelling, while in some cases both spellings are now implemented. (For example DisposPtr and DisposePtr )
  20. Even more important is the treatment of register-based toolbox traps. Most of the memory manager routines and some of the file manager routines are register-based. In MacOberon, you won't be able to use these routines in the same way that you call stack-based ones. You will need to put your variables into the correct registers and read the results from the registers later. The relevant calling conventions can be found in IM under the assembly-language information that always follows the routine description. Example : 
  21. MPW Pascal :                    MacOberon :
  22. myPtr:=NewPtr(theSize);            SYSTEM.PUTREG(0,theSize);
  23.                             ME.NewPtr;
  24.                             SYSTEM.GETREG(8,myPtr);
  25. At first sight, the Oberon version looks clumsy, but when looking at the produced object code, it is evident that MacOberon's output is much more efficient:
  26. MPW Pascal :                    MacOberon :
  27. CLR.L      -(SP)                MOVE.L    theSize,D0
  28. MOVE.L    theSize,-(SP)            _NewPtr
  29. JSR        xy                    MOVE.L    A0,@myPtr
  30. MOVE.L    (SP)+,@myPtr
  31. MOVEA.L    (SP)+,A1
  32. MOVE.L    (SP)+,D0
  33. _NewPtr
  34. MOVE.L    A0,(SP)
  35. JMP        (A1)
  36. In MacOberon, you further need to precede all types and routines by their module name. It is convenient to use the alias import feature of Oberon to abbreviate the names of toolbox modules.
  37. Here are all of the toolbox files supplied in this release, and possible abbreviations:
  38. ME    : MacMemory            ( Memory )
  39. IM    : MacImaging            ( QuickDraw, Printing, Color )
  40. TB    : MacToolbox            ( Menus, Windows, Dialogs, Controls )
  41. MF    : MacFiles                ( Filesystem )
  42. MP    : MacProcesses            ( Processes, Tasks, Interrupts )
  43. MO    : MacOSUtils            ( Gestalt, OS Utilites )
  44. MT    : MoreMacToolbox            ( Resources, Help, Sound )
  45. TE    : MacText                ( TEdit, Textdrawing, Scripts )
  46. MC    : MacIAC                ( Apple Events, Communications )
  47. These toolbox files are not complete. For example, only a small amount of the constants have been declared. Therefore it will be necessary to add your own data to these files, or even better, create new toolbox files containing your additions, so you won't need to update all files when new versions of MacOberon are released.
  48. You absolutly need the corresponding Inside Macintosh volumes when adding new routines, types or constants. It's also helpful if you can get hold of some Interface files of other development platforms as a starting base for creating MacOberon interface files. These files containing all data types and constants, and most routines. When implementing a new routine, just copy the hex code from the interface file. Don't forget the minus right after the PROCEDURE keyword, this tells the compiler that it is dealing with an interface procedure. It will substitute in-line the byte sequence appearing instead of a procedure body when compiling the procedure call. Example :
  49.     Trap only : 
  50.         PROCEDURE- PaintRect*(r:Rect) 
  51.             0A8H,0A2H;
  52.     Trap with routine selector : 
  53.         PROCEDURE- CTabChanged*(port:GrafPtr) 
  54.             020H,03CH,000H,004H,000H,007H,0ABH,01DH;
  55. There are also some routines described in IM, that are not implemented as traps or traps with routine selectors. These are declared as [NOT IN ROM]. There is currently no official way to use these routines from MacOberon. Either you implement the functionality by yourself, or you disassemble the Interface library from another programming environment, such as Interface.o from MPW.
  56. While implementing the toolbox files for MacOberon, simplifications were made to some data types. For example most of the types that are aliases of other simple types were left out. These are for example:
  57.     OSErr = INTEGER    (also QDError)
  58.     Fixed = LONGINT
  59.     Size = LONGINT
  60.     SignedByte = SHORTINT
  61. In some special cases, we also use LONGINT instead of MacMemory.Ptr to ease the use of the address.
  62. The OSType in IM is declared as ARRAY [0..3] OF CHAR, however, this is nothing else than a LONGINT, so when using OSTypes you can pass a LONGINT, for example 'TEXT' = 54455854H. Two characters stand for the hex representation of the ASCII value of that character.
  63. Another important difference is the byte-length of a character. Macintosh toolbox routines always use two bytes for a CHAR instead of one when using MacOberon. Therefore all toolbox routines using CHARs declare their CHARs as INTEGERs. Just pass ORD(myChar) to such routines where myChar is of Oberon type CHAR. Also remember that the function CHR() also returns a single byte CHAR type.
  64. If you want to implement Macintosh datatypes using Ptrs or Handles, follow this scheme :
  65.     myDesc* = RECORD ... END;
  66.     myPtr* = POINTER- TO myDesc;
  67.     myHandle* = POINTER- TO RECORD p*:myPtr END;
  68. The minus after POINTER hides this pointer from garbage collection. Therefore you will need to allocate and deallocate these pointers using NewPtr/DisposePtr, and won't be able to use the Oberon NEW procedure or the garbage collector for these pointers (which is sensible since Oberon cannot know automatically if the Toolbox is still using a pointer). When dereferencing the handle, just use myHandle.p.<myEntry>.
  69. AppLinker
  70. The Application Linker will create a stand_alone Macintosh Application from an Oberon object file. Using it is quite simple. There are, however, some rules to be observed. Because you are using the MacOberon Compiler, the applications produced need at least an 68020 processor. And if you are using REAL or LONGREAL within your application, FPU code is produced, so you have to check for the presence of an FPU by yourself. The check for an 68020 processor will be made by the bootloader when starting the created application.
  71. If you want to use Oberon memory allocation and garbage collection, you also have to be sure that the application gets at least a 1MB memory partition for proper operation.
  72. Usage of the AppLinker:
  73.     AppLinker.Link SampleApp [Creator]
  74. To link an application, you need to provide the module name of your main module. If you specify a creator, the bundle bit will also be set.
  75. To launch an application from within MacOberon for testing purposes, you can use : 
  76.     AppLinker.Launch SampleApp
  77. If there exists a file named SampleApp.res, then all resources from this file are copied to your application. For proper operation you have to include at least a SIZE resource id=-1 with the following flags set to one:
  78.     accept suspend/resume, activate on FG switch and can background
  79. If this resource is not provided, some malfunctions may occur when switching between your and other applications.
  80. The AppLinker also adds two code resources (id 0 and 1) containing the bootloader and two ALRT/DITL combinations (id 32766 / 32767) for the error messages and the trap handler. The object code is stored within the data fork of the application.
  81. Oberon Features
  82. The SYSTEM module contains some useful procedures, some of which are even required to develop Macintosh applications. Most of these procedures are translated into single machine instructions. Check the Oberon language report for more information about SYSTEM's procedures.
  83. The most important procedures for Macintosh applications are:
  84.     SYSTEM.PUTREG(n,x) : Register n := x
  85.     SYSTEM.GETREG(n,v) : v := Register n
  86. IF v is of a real type and 0<=n<=7, then floating point registers are used. Otherwise, 0<=n<=7 stands for data registers D0-D7, 8<=n<=15 for address registers A0-A7.
  87.     SYSTEM.PUT(a,x)    : Mem[a] := x
  88.     SYSTEM.GET(a,v)    : v := Mem[a]
  89.     SYSTEM.MOVE(s,d,n) : Mem[d] 
  90.  Mem[d+n-1] := Mem[s] 
  91.  Mem[s+n-1]
  92.     SYSTEM.VAL(T,x)    : x interpreted as of type T
  93.     SYSTEM.ADR(v)      : address of variable v (LONGINT)
  94. Runtime Library
  95. If you wish to retain the Oberon features of Garbage Collection and Run-Time Type Tests in your Macintosh-like applications, you need to link to an Oberon Runtime Library. Extract and compile the following module Runtime, or include the statements exactly as they appear here in every one of your main programs. Your application will need to call the Garbage Collector at regular intervals.
  96.     MODULE Runtime;    (*mf 8.10.92*)
  97.         IMPORT
  98.             SYS:=SYSTEM;
  99.         TYPE
  100.             Module= POINTER- TO ModDesc;
  101.             ModDesc= RECORD link: Module; data: LONGINT END;
  102.         VAR
  103.             a5: LONGINT; modList: Module; mark1, scan: PROCEDURE;
  104.         PROCEDURE GC*;    (*Call the Garbage Collector*)
  105.             VAR mod: Module;
  106.         BEGIN    mod:=modList;
  107.             WHILE    mod#NIL    DO    SYS.PUTREG(9, mod.data); mark1;
  108.                 mod:=mod.link
  109.             END;
  110.             scan
  111.         END GC;
  112.     BEGIN    SYS.GETREG(13, a5);
  113.         SYS.GET(a5-24, modList); SYS.GET(a5-20, mark1); SYS.GET(a5-16, scan)
  114.     END Runtime.
  115. Runtime exceptions are generated to vector number 7 (TRAPcc), and array index check exceptions are compiled into the CHK instruction which has vector number 6. You can install a trap handler of your own in these slots. If you don't, MacsBug will handle the exception if installed. The TRAPcc.W instruction is used with a word parameter signifying the source of the trap as follows:
  116.       2    NIL-reference (/n compiler option disables NIL-checking)
  117.     16    Invalid case in CASE statement
  118.     17    Function procedure without RETURN statement
  119.     18    Type guard check (/t compiler option disables type-checking)
  120.     19    Implied type guard check in record assignment
  121.     23    Out of heap space (when Oberon's NEW is used, not NewPtr)
  122. Use the /x compiler option to turn off array index range checking.
  123. Examples
  124. There are two examples containing Macintosh toolbox routines. The first one is a small standalone text editor using the built-in Macintosh TextEdit, that can be used as a framework for other applications.
  125.     Edit.Open SampleApp.Mod
  126. The second one is an example of using the Oberon System and the Macintosh Interface together. It has only one command, Text, that uses a Macintosh file selector box to choose a plain text file and then opens a TextViewer containing the text from the file. You can use this command to import text files from other directories into MacOberon.
  127.     Edit.Open Import.Mod
  128. Tips & Hints
  129. Here is a list of usful hints that may not be so obvious to novice users:
  130. -    Use the brower '+' option if you are not sure how your data types are allocated. This is useful if you are implementing new Toolbox datatypes. You will be given the offsets of the record entries and therefore you can calculate the size of these entries. See the Brower.Tool for more information about the Browser.
  131. -    For string conversion between Oberon strings (null terminated) and Macintosh Pascal strings, you can use SetStr255 and GetStr255 from the MacToolbox module.
  132. -    When using CASE constructs, use the ELSE path for unwanted results. Otherwise traps may occur.
  133. -    When system error id=28 occurs, the application heap and the stack collided. Therefore, you have to increase the memory partition of your program or adjust the stack size. (See Inside Macintosh Memory for adjusting the stack size)
  134. -    If you are using REAL or LONGREAL operations, please don't forget to check for the presence of an FPU processor, otherwise your program just crashes on machines without an FPU (or an FPU emulator).
  135. Further Reading
  136. - Inside Macintosh Volume I, Chapter 4 (Assembly Language)
  137. - Inside Macintosh Toolbox Essentials (for SampleApp )
  138. - Oberon Guide (for Oberon System) 
  139. - Oberon Report (for Oberon programming)
  140. Request for Comment
  141. This is the second release of the ApplLinker. It seems to work fine, but the authors can assume no warranties. An advantage of the approach taken by MacOberon (BootLoader with link fixup) is that none of the usual restrictions of the Macintosh application architecture apply to applications created with the linker. For example, each module can have as much global data as required, and the code image can be of arbitrary size.
  142. If you use this Linker, we would greatly appreciate your feedback to "franz@inf.ethz.ch".
  143.